﻿namespace SD.DataAccess
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.SqlClient;

    /// <summary>
    /// organization Data Access Layer
    /// </summary>
    public class OrganizationDao
    {
        /// <summary>
        /// Data Context
        /// </summary>
        private readonly ServiceDBDataContext _context = new ServiceDBDataContext();

        /// <summary>
        /// Adds the organization.
        /// </summary>
        /// <param name="organization">The organization.</param>
        /// <returns>true/false result</returns>
        public bool AddOrganization(Organization organization)
        {
            try
            {
                _context.Organizations.InsertOnSubmit(organization);
                _context.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Adds the organization program.
        /// </summary>
        /// <param name="list">The organization programs.</param>
        /// <returns>true/false result</returns>
        public bool AddOrganizationProgram(IEnumerable<OrganizationProgram> list)
        {
            try
            {
                _context.OrganizationPrograms.InsertAllOnSubmit(list);
                _context.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Edits the organization program.
        /// </summary>
        /// <param name="organizationId">The organization id.</param>
        /// <param name="list">The organization program.</param>
        /// <returns>true/false result</returns>
        public bool EditOrganizationProgram(int organizationId, ICollection<OrganizationProgram> list)
        {
            try
            {
                    var editedList = from op in _context.OrganizationPrograms
                                     where op.OrganizationId == organizationId
                                     select op;

                    OrganizationProgram objOp = null;
                    foreach (var program in editedList)
                    {
                        objOp = list.FirstOrDefault(op => op.ProgramId == program.ProgramId);
                        if (objOp == null)
                        {
                            _context.OrganizationPrograms.DeleteOnSubmit(program);
                            _context.SubmitChanges();
                            continue;
                        }

                        list.Remove(objOp);
                    }

                    _context.OrganizationPrograms.InsertAllOnSubmit(list);
                    _context.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return false;
        }

        /// <summary>
        /// Edits the organization.
        /// </summary>
        /// <param name="editedOrganization">The edited organization.</param>
        /// <returns>true/false result</returns>
        public bool EditOrganization(Organization editedOrganization)
        {
            try
            {
                var organization = GetOrganizationById(editedOrganization.OrganizationId);
                if (organization != null)
                {
                    // Main property
                    organization.OrganizationName = editedOrganization.OrganizationName;
                    organization.ShortDescription = editedOrganization.ShortDescription;
                    organization.FullDescription = editedOrganization.FullDescription;
                    organization.CityTown = editedOrganization.CityTown;
                    organization.County = editedOrganization.County;
                    organization.PreferredOrganization = editedOrganization.PreferredOrganization;
                    organization.PhoneNumber = editedOrganization.PhoneNumber;
                    organization.Fax = editedOrganization.Fax;
                    organization.Email = editedOrganization.Email;
                    organization.WebAddress = editedOrganization.WebAddress;
                    organization.CharityNumber = editedOrganization.CharityNumber;
                    organization.CompanyNumber = editedOrganization.CompanyNumber;
                    organization.CountryId = editedOrganization.CountryId;

                    // Foreign key properties
                    organization.ContactId = editedOrganization.ContactId;
                    organization.BusinessId = editedOrganization.BusinessId;
                    organization.AddressId = editedOrganization.AddressId;

                    // Reference properties
                    organization.OrganizationSpecialism = editedOrganization.OrganizationSpecialism;
                    organization.ServiceDisabilities = editedOrganization.ServiceDisabilities;
                    organization.ServiceBarriers = editedOrganization.ServiceBarriers;
                    organization.ServiceBenefits = editedOrganization.ServiceBenefits;
                    organization.ServiceCircumstances = editedOrganization.ServiceCircumstances;
                    organization.ServiceEthnicity = editedOrganization.ServiceEthnicity;
                    organization.Accreditation = editedOrganization.Accreditation;
                    _context.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }

        /// <summary>
        /// Sets the active organization.
        /// </summary>
        /// <param name="organizationId">The organization id.</param>
        /// <param name="status">if set to <c>true</c> [status].</param>
        /// <returns></returns>
        public bool SetActiveOrganization(int organizationId, bool status)
        {
            var organization = _context.Organizations.FirstOrDefault(o => o.OrganizationId == organizationId);
            if (organization != null)
            {
                organization.IsActive = status;
                _context.SubmitChanges();
                return true;
            }

            return false;
        }

        /// <summary>
        /// Gets the organization by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>organization object</returns>
        public Organization GetOrganizationById(int id)
        {
            return _context.Organizations.FirstOrDefault(o => o.OrganizationId == id);
        }

        /// <summary>
        /// Gets the organization ref by category id.
        /// </summary>
        /// <param name="categoryId">The category id.</param>
        /// <returns>collection of organization Ref.</returns>
        public IQueryable<OrganizationReference> GetReferencesByCategoryId(int categoryId)
        {
            var query = _context.OrganizationReferences.Where(o => o.Category == categoryId);
            return query;
        }

        /// <summary>
        /// Determines whether [is name exists].
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>
        /// <c>true</c> if [is name exists]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsNameExists(string name)
        {
            var result = _context.Organizations.FirstOrDefault(o => o.OrganizationName.Equals(name));
            return result != null;
        }

        /// <summary>
        /// Gets the organization program by organisaton id.
        /// </summary>
        /// <param name="organizationId">The organization id.</param>
        /// <returns></returns>
        public IQueryable<OrganizationProgram> GetOrganizationProgramByOrganizationId(int organizationId)
        {
            var query = _context.OrganizationPrograms.Where(o => o.OrganizationId == organizationId);
            return query;
        }
    }
}
